<?php
/**
 * User: day
 * Date: 2018/8/18
 * Time: 20:00
 * Desc: 删除试玩相关信息
 */

namespace crontab;



use App\Models\TrialOfflineAgentUser;
use App\Models\TrialOrderSub;
use App\Models\TrialFunds;
use App\Models\TrialSendPrize;
use App\Models\TrialUser;
use Illuminate\Support\Facades\Log;

class DeleteTrial {
    /**
     * 脚本入口
    */
    public function handle($argv)
    {
        Log::info("试玩数据 开始处理");
        try {
            $this->deleteTrialData();
        } catch(\Throwable $e) {
            $msg = "请修复程序再运行:\nMessage:" . $e->getMessage() . "\nFile:" . $e->getFile() . "\nLine:" . $e->getLine() . "\ntrace: " . $e->getTraceAsString() . PHP_EOL;
            Log::info($msg);
            BaseHandler::error($msg);
        }
        Log::info("试玩数据 处理完成");
    }

    /**
     * 删除试玩相关信息
     */
    public function deleteTrialData()
    {
        $user_value = TrialUser::select("created,id,name,wallet_id")->where(['!=', 'channel', 'admin'])->get()->toArray();
        if (empty($user_value)) {
            return;
        }

        foreach ($user_value as $k => $v) {
            $user_id = $v['id'];
            $user_name = $v['name'];
            $wallet_id = $v['wallet_id'];
            $created_time = strtotime($v['created']);
            $days = round((time() - $created_time) / 60*60*24);
            if ($days >= 3) {
                TrialOfflineAgentUser::deleteAll(['user_id' => $user_id]);
                TrialOrderSub::deleteAll(['uid' => $user_id]);
                TrialFunds::deleteAll(['id' => $wallet_id]);
                TrialSendPrize::deleteAll(['user_id' => $user_id, 'user_name' => $user_name]);
                TrialUser::deleteAll(['id' => $user_id, 'name' => $user_name]);
            }
        }
    }
}